package com.eds.rabbitmeq.utils;

import com.eds.pojo.MyMessage;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Created with IntelliJ IDEA.
 *
 * @ClassName：SendMessageUtils
 * @Author: vvSmile
 * @Date: 2025/04/08
 * @Description: 必须描述类做什么事情, 实现什么功能
 */
@Component
public class SendMessageUtils {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    //发送异步消息
    public void sendMessage(String exchange, String routingKey, MyMessage message) {
        //设施消息属性
        MessagePostProcessor processor = msg -> {
            //设置消息持久化
            msg.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
            //设置消息过期时间
            msg.getMessageProperties().setExpiration("10000");
            return msg;
        };
        rabbitTemplate.convertAndSend(exchange, routingKey, message, processor);
    }

    //发送同步消息
    public Object sendAndReceive(String exchange, String routingKey, MyMessage message) {
        return rabbitTemplate.convertSendAndReceive(exchange, routingKey, message);
    }
}
